/*
Copyright 2010 Johan Maasing

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package nu.zoom.android.gdata.project.client;

import nu.zoom.android.accounts.AccountManagerWrapper;
import android.accounts.Account;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class StandardMenuHandler {
	private final static String LOG_TAG = WKC.LOG_TAG;
	private final Activity activity;
	private final AccountManagerWrapper accountManagerWrapper;

	public StandardMenuHandler(final Activity activity) {
		this.activity = activity;
		this.accountManagerWrapper = new AccountManagerWrapper(activity);
	}

	public boolean onCreateOptionsMenu(final Menu menu) {

		MenuInflater inflater = this.activity.getMenuInflater();
		inflater.inflate(R.menu.application, menu);
		return true;
	}

	public boolean onOptionsItemSelected(final MenuItem item) {

		switch (item.getItemId()) {
		case R.id.aboutMenuItem:
			Intent aboutIntent = new Intent(
					"org.openintents.action.SHOW_ABOUT_DIALOG");
			try {
				activity.startActivityForResult(aboutIntent, 0);
				return true;
			} catch (ActivityNotFoundException e) {
				Log.e(LOG_TAG,
						"OI About not installed, will try fallback dialog.");
				activity.showDialog(WKC.DialogID.ABOUT);
				return true;
			}
		case R.id.settingsMenuItem:
			Intent intent = Intents.settings(activity);
			activity.startActivity(intent);
			return true;
			/*
		case R.id.signinMenuItem:
			activity.showDialog(WKC.DialogID.SELECT_ACCOUNT);
			return true;
			*/
		default:
			return false;
		}
	}

	public Dialog onCreateDialog(final int id) {
		switch (id) {
		case WKC.DialogID.ABOUT:
			return new FallbackAboutDialog(activity);
		case WKC.DialogID.SELECT_ACCOUNT:
			return createAccountSelectDialog();
		default:
			return null;
		}
	}

	private Dialog createAccountSelectDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this.activity);
		builder.setTitle("Select a Google account");
		final Account[] accounts = this.accountManagerWrapper.getAccounts();
		int preferredAccountIndex = -1;
		final String preferredAccountName = this.accountManagerWrapper
				.getPreferredAccountName();
		final int size = accounts.length;
		String[] names = new String[size];
		for (int i = 0; i < size; i++) {
			names[i] = accounts[i].name;
			if (preferredAccountName != null) {
				if (preferredAccountName.equals(names[i])) {
					Log.d(WKC.LOG_TAG, "Preferred account has index: " + i);
					preferredAccountIndex = i;
				}
			}
		}
		builder.setSingleChoiceItems(names, preferredAccountIndex,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						accountManagerWrapper
								.setPreferredAccountNameAndNotify(accounts[which]);
					}
				});
		return builder.create();
	}
}
